from django.conf import settings
from itsdangerous import TimedJSONWebSignatureSerializer, BadData


# 加密openid
def generate_secret_openid(openid):
    serializer = TimedJSONWebSignatureSerializer(settings.SECRET_KEY,
                                                 expires_in=600)

    data = {'openid': openid}
    secret_openid = serializer.dumps(data).decode()

    return secret_openid


def check_secret_openid(secret_openid):
    serializer = TimedJSONWebSignatureSerializer(settings.SECRET_KEY)
    try:
        data = serializer.loads(secret_openid)
    except BadData:
        return None

    return data.get('openid')
